package org.contextsensitivetodo.context.configuration;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.contextsensitivetodo.ShowContextActivity;
import org.contextsensitivetodo.context.ContextDelivery;
import org.contextsensitivetodo.context.ContextStateChangeListener;
import org.contextsensitivetodo.context.ContextStateChangedEvent;
import org.contextsensitivetodo.context.UserContext;

import android.os.Handler;
import android.text.format.Time;
import android.util.Log;
import android.widget.TextView;

public class UserContextConfiguration {

	// temp:
	private TextView textViewForDebugOutput;
	private ShowContextActivity showContextActivity;

	private Handler handler;

	private String lastUpdateTimeStamp = "unknown time";

	private static UserContextConfiguration instance = new UserContextConfiguration();

	private LinkedList<UserContext> contexes = new LinkedList<UserContext>();

	private UserContextConfiguration() {
	}

	public static UserContextConfiguration getInstance() {
		return instance;
	}
	
	public UserContext getUserContextByName(String name){
		for (UserContext context : contexes) {
			if((""+name).equals(context.getName())){
				return context;
			}
		}
		return null;
	}

	private ContextStateChangeListener stateChangedListener = new ContextStateChangeListener() {

		public void handleContextStateChanged(ContextStateChangedEvent e) {
			Log.d("cstodo", "*** User Context config context change detected!");
			contextChanged();
		}
	};

	public String getLastUpdateTimeStamp() {
		return lastUpdateTimeStamp;
	}

	public void setupAddNewContext(UserContext newContext) {
		this.contexes.add(newContext);
		newContext.addContextRuleStateChangeListener(this.stateChangedListener);
	}

	public LinkedList<UserContext> getOnContext() {
		LinkedList<UserContext> ret = new LinkedList<UserContext>();
		for (UserContext context : contexes) {
			if (context.isOn()) {
				ret.add(context);
			}
		}
		return ret;
	}

	public LinkedList<UserContext> getOffContext() {
		LinkedList<UserContext> ret = new LinkedList<UserContext>();
		for (UserContext context : contexes) {
			if (!context.isOn()) {
				ret.add(context);
			}
		}
		return ret;
	}

	public List<UserContext> getAllContext() {
		Log.d("cstodo", "returning all contexes: "
				+ Collections.unmodifiableList(this.contexes));

		return Collections.unmodifiableList(this.contexes);
	}

	private void contextChanged() {

		this.handler.post(new Runnable() {
			public void run() {
				if (textViewForDebugOutput != null) {
					Time now = new Time();
					now.setToNow();

					lastUpdateTimeStamp = ""
							+ (now.hour)
							+ ":"
							+ (now.minute < 10 ? "0" + now.minute
									: (now.minute));

					Log.d("cstodo", "#### updating textfield");
					textViewForDebugOutput.setText(ContextDelivery.getInstace()
							.getCurrentContextAsTextForDebug());
					textViewForDebugOutput.invalidate();

					showContextActivity.setUserContextList(ContextDelivery
							.getInstace().getContextAsList());
				}
			}
		});

	}

	public void setOutputTextForDebug(TextView textView) {
		this.textViewForDebugOutput = textView;
	}

	public void setUIThreadCallBackHandler(Handler handler) {
		this.handler = handler;
	}

	public void setShowContextActivity(ShowContextActivity showContextActivity) {
		this.showContextActivity = showContextActivity;
	}
}
